package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Activity;
import android.app.Application;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.MyApp;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.AppConstant;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.Tencent;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.InviteEntity;
import com.ysg.http.data.entity.mine.InviteIncomeEntity;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class MineInviteVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> code = new ObservableField<>();
    public InviteEntity inviteEntity;
    public IWXAPI wxApi;
    private String shareUrl;

    public MineInviteVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
        wxApi = WXAPIFactory.createWXAPI(getApplication(), AppConstant.APPID_WECHAT);
        wxApi.registerApp(AppConstant.APPID_WECHAT);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onInviteInfoEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onTabEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onIncomeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onPersonEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRankEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onShareEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onCopyCommand = new BindingCommand(()-> onCopy());
    public BindingCommand onIncomeCommand = new BindingCommand(()-> uc.onTabEvent.setValue(0));
    public BindingCommand onInvitePeopleCommand = new BindingCommand(()-> uc.onTabEvent.setValue(1));
    public BindingCommand onRankCommand = new BindingCommand(()-> uc.onTabEvent.setValue(2));
    public BindingCommand onShareCommand = new BindingCommand(()-> uc.onShareEvent.call());



    //=========================================接口：start===========================================
    /**
     *
     */
    public void requestInviteInfo(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.inviteInfo(map), this,
                        new OnSuccessResult<InviteEntity>() {
                            @Override
                            public void onSuccessResult(BaseResponse<InviteEntity> response) {
                                uc.onInviteInfoEvent.setValue(response.getData());
                                inviteEntity = response.getData();
                                shareUrl = "http://api.ptszyxx.com:8080/register/index.html?inviteCode=" + inviteEntity.getYaoqingma();
                            }
                        });
    }
    /**
     *
     */
    public void requestInviteIncome(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.invite1(map), this,
                        new OnSuccessListResult<InviteIncomeEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<InviteIncomeEntity> response) {
                                uc.onIncomeEvent.setValue(response.getData());
                            }
                        });
    }
    /**
     *
     */
    public void requestInvitePeople(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.invite2(map), this,
                        new OnSuccessListResult<InviteIncomeEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<InviteIncomeEntity> response) {
                                uc.onPersonEvent.setValue(response.getData());
                            }
                        });
    }
    /**
     *
     */
    public void requestInviteRank(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.invite3(map), this,
                        new OnSuccessListResult<InviteIncomeEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<InviteIncomeEntity> response) {
                                uc.onRankEvent.setValue(response.getData());
                            }
                        });
    }

    /**
     * 复制邀请码
     */
    private void onCopy() {
        if (inviteEntity != null && YStringUtil.isNotEmpty(inviteEntity.getYaoqingma())){
            ClipboardManager cmb = (ClipboardManager) MyApp.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);
            cmb.setText(inviteEntity.getYaoqingma());
            YToastUtil.showShort(R.string.copy);
        }
    }


    public void wechatShare(int flag, String title, String content) {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl= "http://api.ptszyxx.com:8080/register/index.html?inviteCode=" + inviteEntity.getYaoqingma();
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = content;
        //演示替换一张自己工程里的图片资源
        Bitmap thumb = BitmapFactory.decodeResource(getApplication().getResources(), R.mipmap.icon_logo);
        msg.setThumbImage(thumb);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        wxApi.sendReq(req);
    }


    public Tencent mTencent;
    public void qqShare(Activity activity, String title, String content, String appName) {
        mTencent = Tencent.createInstance(AppConstant.APPID_QQ, getApplication(), AppConstant.FILE_PROVIDER);
        Tencent.setIsPermissionGranted(true);
        final Bundle params = new Bundle();
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "");
        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, appName);
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        mTencent.shareToQQ(activity, params, null);
    }



}
